import request, { request as requestMethods } from '@/utils/request';

/**
 * 获取客服列表
 */
export function getServiceList() {
    return requestMethods({
        url: '/chat/service/service/list',
        method: 'GET'
    });
}

/**
 * 获取客服详情
 * @param {number} serviceId - 客服ID
 */
export function getServiceInfo(serviceId) {
    return requestMethods({
        url: `/chat/service/service/${serviceId}`,
        method: 'GET'
    });
}

/**
 * 更新客服状态
 * @param {string} status - 状态 (ONLINE/BUSY/OFFLINE)
 */
export function updateServiceStatus(status) {
    return requestMethods({
        url: '/chat/service/status',
        method: 'POST',
        data: { status }
    });
}

/**
 * 获取可用的客服列表
 */
export function getAvailableServices() {
    return requestMethods({
        url: '/chat/service/service/available',
        method: 'GET'
    });
}

/**
 * 获取客服统计数据
 * @param {string} startDate - 开始日期
 * @param {string} endDate - 结束日期
 */
export function getServiceStats(startDate, endDate) {
    return requestMethods({
        url: '/chat/service/stats',
        method: 'GET',
        params: { startDate, endDate }
    });
}

/**
 * 检查用户是否是客服
 */
export function checkIsCustomerService() {
    return requestMethods.get('/chat/service/check');
}

/**
 * 获取在线客服列表
 */
export function getOnlineServices() {
    return requestMethods.get('/chat/service/service/online');
}

/**
 * 获取客服详情
 * @param {number} id - 客服ID
 */
export function getServiceById(id) {
    return requestMethods.get(`/chat/service/service/${id}`);
}

/**
 * 获取客服的状态
 * @param {number} id - 客服ID
 */
export function getServiceStatus(id) {
    return requestMethods.get(`/chat/service/status?serviceId=${id}`);
}

/**
 * 获取客服会话列表
 */
export function getServiceSessions() {
    return requestMethods.get('/chat/service/sessions');
}

/**
 * 分页获取客服列表
 * @param {Object} params - 查询参数
 * @param {number} params.current - 当前页码
 * @param {number} params.size - 每页大小
 * @param {string} [params.keyword] - 搜索关键词
 * @param {string} [params.serviceType] - 客服类型
 */
export function getServicePage(params) {
    // 确保参数都是正确类型
    const queryParams = {
        current: params.current || 1,
        size: params.size || 10
    };

    // 只添加非空的可选参数
    if (params.keyword && params.keyword.trim()) {
        queryParams.keyword = params.keyword.trim();
        console.log('Adding keyword parameter:', queryParams.keyword);
    }
    if (params.serviceType) {
        queryParams.serviceType = params.serviceType;
        console.log('Adding serviceType parameter:', queryParams.serviceType);
    }
    if (params.groupId) {
        queryParams.groupId = params.groupId;
        console.log('Adding groupId parameter:', queryParams.groupId);
    }

    console.log('Calling getServicePage with final params:', queryParams);
    
    return requestMethods({
        url: '/chat/service/service/page',
        method: 'GET',
        params: queryParams
    }).then(response => {
        console.log('getServicePage response:', response);
        if (!response.data) {
            console.warn('Response data is empty');
        } else if (!response.data.list) {
            console.warn('Response data.list is empty');
        }
        return response;
    }).catch(error => {
        console.error('getServicePage error:', error);
        console.error('Error details:', {
            status: error.response?.status,
            message: error.response?.data?.message || error.message,
            config: error.config
        });
        throw error;
    });
}

export default {
    getServiceList,
    getServiceInfo,
    updateServiceStatus,
    getAvailableServices,
    getServiceStats,
    checkIsCustomerService,
    getOnlineServices,
    getServiceById,
    getServiceStatus,
    getServiceSessions,
    getServicePage
}; 